package MyLinkQueue;

public class MyLinkQueue <T> implements IQueue<T>{
   static class ListNode<T>{
       public T val;
       public ListNode<T> next;
       public ListNode<T> prev;

       public ListNode(T val)
       {
           this.val = val;
       }
   }

   private ListNode<T> head;
   private ListNode<T> last;
   private int usedSize;
    @Override
    public boolean offer(T val) {
        ListNode<T> node = new ListNode<>(val);
        if(head == null)
        {
            head = node;
            last = node;
        }
        else
        {
            last.next = node;
            node.prev = last;
            last = last.next;
        }
        usedSize++;
        return true;
    }

    @Override
    public T poll() {
        if(head == null) return null;

        T ret = head.val;
        if(head.next == null)
        {
            head = null;
            last = null;
            usedSize--;
            return ret;
        }
        head = head.next;
        head.prev = null;
        usedSize--;
        return ret;
    }

    @Override
    public T peek() {
        if(head == null) return null;
        return head.val;
    }

    @Override
    public boolean empty() {
        return head == null;
    }

    @Override
    public int size() {
        return usedSize;
    }
}
